package com.sgb.contract.view.ui.approval;

import com.sgb.contract.api.AbstractCallBack;
import com.sgb.contract.api.ContractNet;
import com.sgb.contract.base.viewmodel.BaseViewModel;
import com.sgb.contract.model.ContractApprovalReBean;
import com.sgb.contract.model.ContractApprovalStatusBean;
import com.sgb.contract.model.ContractDetailBean;
import com.sgb.contract.model.ContractOrganizationBean;
import com.sgb.contract.model.ContractUserMsgBean;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.List;

import androidx.lifecycle.MutableLiveData;
import retrofit2.Call;

/**
 * 描述:合同-审批申请
 *
 * @author qiangHeNoo
 * <p>
 * 时间:2021.11.03
 */
public class ApprovalModel extends BaseViewModel {

    public MutableLiveData<Boolean> showCompanyToastMessage = new MutableLiveData<>();
    public MutableLiveData<ContractUserMsgBean> businessInfoByCompanyBeanMutableLiveData = new MutableLiveData<>();

    public MutableLiveData<Boolean> showDepartmentToastMessage = new MutableLiveData<>();
    public MutableLiveData<List<ContractOrganizationBean>> departmentBeanList = new MutableLiveData<>();

    public MutableLiveData<Boolean> showDepartmentToastMessage1 = new MutableLiveData<>();

    /**
     * 企业信息
     */
    public void getBusinessInfoByCompany() {
        showLoading();
        ContractNet.getInstance().getContractApi().getUserInfo().enqueue(new ICallBack<BaseEntity<ContractUserMsgBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractUserMsgBean> data) {
                if (data == null || data.getData() == null) {
                    showCompanyToastMessage.postValue(true);
                } else {
                    businessInfoByCompanyBeanMutableLiveData.postValue(data.getData());
                }
            }
        });
    }

    /**
     * 获取公司部门信息
     */
    public void getOrganizationDepartment() {
        ContractNet.getInstance().getCapitalApi().getOrganizationDepartment().enqueue(new ICallBack<BaseEntity<List<ContractOrganizationBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<ContractOrganizationBean>> data) {
                if (data == null || data.getData() == null || data.getData().size() == 0) {
                    showDepartmentToastMessage.postValue(true);
                } else {
                    departmentBeanList.postValue(data.getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<ContractOrganizationBean>>> call, Throwable t) {
                showDepartmentToastMessage.postValue(true);
            }
        });
    }

    /**
     * 校验合同状态
     */
    public void checkStatusByNowStatus(ContractDetailBean bean) {
        showLoading();
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus,bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        /*当前签署合同才有审批*/
                        checkApprovalOpenAudit("signContract", bean);
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        showDialog("checkStatusByNowStatus", entity.getMsg());
                    }
                });
    }

    /**
     * 是否开启审批
     *
     * @param auditType 审批类型：发送审批 sendContract 签署审批 signContract
     */
    private void checkApprovalOpenAudit(String auditType, ContractDetailBean bean) {
        ContractNet.getInstance().getContractApi().checkOpenAudit(auditType)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        if (data.getData()) {
                            findAuditStatus(bean, "signContract");
                        } else {
                            showDialog("checkApprovalOpenAudit", "审核状态已发生变化，请刷新\n" + "页面后再操作");
                        }
                    }
                });
    }

    /**
     * 查询合同的审批状态
     *
     * @param bean      合同id
     * @param auditType auditType 审批类型：发送审批 sendContract 签署审批 signContract
     */
    private void findAuditStatus(ContractDetailBean bean, String auditType) {
        ContractNet.getInstance().getContractApi().findAuditStatus(bean.id, auditType).enqueue(new AbstractCallBack<BaseEntity<ContractApprovalStatusBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractApprovalStatusBean> data) {
                if (data.getData().auditStatus == bean.appAuditStatus) {
                    showDepartmentToastMessage1.postValue(true);
                } else {
                    showDialog("checkApprovalOpenAudit", "审核状态已发生变化，请刷新\n" + "页面后再操作");
                }
            }
        });
    }


    /**
     * 发起审批
     */
    public void sendContractApprovalProcess(ContractApprovalReBean contractApprovalProcess) {
        ContractNet.getInstance().getContractApi().contractApproval(contractApprovalProcess).enqueue(new AbstractCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                showDialog("sendContractApprovalProcess", "申请成功");
            }

            @Override
            public void onBusinessFailure(BaseEntity entity) {
                showDialog("", entity.getMsg());
            }
        });
    }

} 